<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏 -->
			<view :style="{ height: statusBarHeight + 'px' }"></view>
			<!-- 导航内容 -->
			<view class="navbar-content" :style="{ height: navBarHeight + 'px', width: windowWidth + 'px'}" :class="{search: isSearch}" @click="open">
				<view class="navbar-content__search-icons">
					<uni-icons type="back" size="22" color="#fff" @click="back"></uni-icons>
				</view>
				<view class="navbar-search" v-if="!isSearch">
					<view class="navbar-search_icon">
						<text class="iconfont icon-sousuosearch82 icon-size"></text>
					</view>
					<view class="navbar-search_text">uni-app</view>
				</view>
				<view class="navbar-search" v-else>
					<input class="navbar-search_text" type="text" v-model="value" placeholder="请输入你要搜索的内容" @input="inputChange"/>
				</view>
			</view>
		</view>
		<view :style="{height: statusBarHeight + navBarHeight +'px'}"></view>
	</view>
</template>

<script>
	export default {
		props: {
			isSearch: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				statusBarHeight: 20,
				navBarHeight: 45,
				windowWidth: 375,
				value: ''
			};
		},
		created () {
		// 获取状态栏高度
		 const info = uni.getSystemInfoSync()
		 this.statusBarHeight = info.statusBarHeight
		 // console.log(info.windowWidth)
		 this.windowWidth = info.windowWidth
		  // #ifndef H5 || APP-PLUS || MP-ALIPAY
		 // 获取胶囊的位置
		const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
		const navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) +  (menuButtonInfo.top - info.statusBarHeight)
		this.windowWidth = menuButtonInfo.left
		console.log(menuButtonInfo)
		// #endif
		},
		methods: {
			open () {
				if (this.isSearch) return
				uni.navigateTo({
					url: "/pages/home-search/home-search"
				})
			},
			inputChange (e) {
				const { value } = e.detail
				this.$emit('change', value)
			},
			back () {
				uni.switchTab({
					url: '/pages/tabbar/index/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../common/css/icons.css';
	.navbar {
		.navbar-fixed {
			width: 100%;
			background-color: $mk-base-color;
			box-sizing: border-box;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			.navbar-content {
				height: 45px;
				padding: 0 15px;
				display: flex;
				justify-content: center;
				align-items: center;
				box-sizing: border-box;
				.navbar-search {
					display: flex;
					align-items: center;
					padding: 0 10px;
					width: 100%;
					height: 30px;
					border-radius: 30px;
					background-color: #fff;
					.navbar-search_icon {
						margin-right: 10px;
						.icon-size {
							font-size: 22px;
						}
					}
					.navbar-search_text {
						width: 100%;
						font-size: 13px;
						color: #999;
					}
				}
				&.search {
					padding-left: 0;
					.navbar-content__search-icons {
						margin-left: 10px;
						margin-right: 10px;
					}
					.navbar-search {
						border-radius: 5px;
					}
				}
			}
		}
	}
</style>
